import java.io.*;

import java.awt.geom.Point2D;
import java.text.*;
import java.math.*;
import java.util.*;

public class Main implements Runnable {

	final String filename = "in";

	public static void main(String[] args) {
		// new Thread(new Main()).start();
		new Thread(null, new Main(), "1", 1 << 25).start();
	}

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new BufferedWriter(new OutputStreamWriter(System.out));
			//in = new BufferedReader(new FileReader(filename));
			// out = new BufferedWriter(new FileWriter(filename+".out"));
			int kase = iread();
			for (int i = 1; i <= kase; ++i) {
				solve(i);
			}
			out.flush();
		} catch (Exception e) {
			System.exit(1);
		}
	}

	int[] nums;
	int numCount;
	int[][][] dp;

	public void solve(int kaseno) throws Exception {
		numCount = iread();
		nums = new int[numCount];
		dp = new int[numCount + 1][numCount + 1][numCount + 1];
		for (int i = 0; i < numCount; ++i)
			nums[i] = iread();
		for (int i = 0; i <= numCount; ++i) {
			for (int j = 0; j <= numCount; ++j) {
				Arrays.fill(dp[i][j], -1);
			}
		}
		// debug(nums);
		int ret = go(0, numCount - 1, 1);
		out.write("Case " + kaseno + ": " + ret + "\n");
	}

	int go(int l, int r, int d) {
		if (l == r) {
			return d * d;
		}
		if (l > r) {
			return 0;
		}
		if (dp[l][r][d] != -1)
			return dp[l][r][d];
		int bst = go(l + 1, r, 1) + (d * d);
		for (int i = l + 1; i <= r; ++i) {
			if (nums[i] == nums[l]) {
				int v1 = go(i, r, d + 1);
				int v2 = go(l + 1, i - 1, 1);
				bst = Math.max(bst, v1 + v2);
			}
		}
		return dp[l][r][d] = bst;
	}

	public void debug(Object... o) {
		System.err.println(Arrays.deepToString(o));
	}

	public int iread() throws Exception {
		return Integer.parseInt(readword());
	}

	public double dread() throws Exception {
		return Double.parseDouble(readword());
	}

	public long lread() throws Exception {
		return Long.parseLong(readword());
	}

	BufferedReader in;
	BufferedWriter out;

	public String readword() throws IOException {
		StringBuilder b = new StringBuilder();
		int c;
		c = in.read();
		while (c >= 0 && c <= ' ') {
			c = in.read();
		}
		if (c < 0) {
			return "";
		}
		while (c > ' ') {
			b.append((char) c);
			c = in.read();
		}
		return b.toString();
	}
}
